package com.yingpai.fitness.dvp_2_0.main.activity.mine.shop_refund;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.GsonUtil;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.bean.CompanyListBean;
import com.yingpai.fitness.dvp_2_0.main.bean.shop_refund.RefundServiceBean;
import com.yingpai.fitness.dvp_2_0.main.bean.shop_refund.RefundServiceDetailBean;
import com.yingpai.fitness.dvp_2_0.main.util.DialogUtil;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.round.RCRelativeLayout;
import com.yingpai.fitness.entity.ShopDetailMcipChildBean;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2018/11/28.
 */

public class RefundServiceDetailActivity extends CommonActivity {
    @BindView(R.id.status)
    TextView status;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.message)
    TextView message;
    @BindView(R.id.photo)
    ImageView photo;
    @BindView(R.id.rc_layout)
    RCRelativeLayout rcLayout;
    @BindView(R.id.item_title)
    TextView itemTitle;
    @BindView(R.id.num_spec)
    TextView numSpec;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.reason)
    TextView reason;
    @BindView(R.id.money_tv)
    TextView moneyTv;
    @BindView(R.id.refund_time)
    TextView refundTime;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.instruction_line)
    View instructionLine;
    @BindView(R.id.instruction)
    TextView instruction;
    @BindView(R.id.instruction_ll)
    LinearLayout instructionLl;
    @BindView(R.id.address_line)
    View addressLine;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.address_rl)
    RelativeLayout addressRl;
    @BindView(R.id.img1)
    ImageView img1;
    @BindView(R.id.img2)
    ImageView img2;
    @BindView(R.id.img3)
    ImageView img3;
    @BindView(R.id.take_photo_ll)
    LinearLayout takePhotoLl;
    @BindView(R.id.logistics)
    TextView logistics;
    @BindView(R.id.select_tv)
    TextView selectTv;
    @BindView(R.id.logistics_num)
    EditText logisticsNum;
    @BindView(R.id.logistics_ll)
    LinearLayout logisticsLl;
    @BindView(R.id.logistic)
    TextView logistic;
    @BindView(R.id.logistics_nu)
    TextView logisticsNu;
    @BindView(R.id.logistics_l)
    LinearLayout logisticsL;
    @BindView(R.id.submit)
    Button submit;
    private QuickAdapter<CompanyListBean.DataBean> quickAdapter;
    private String refundId;
    private String refundMark;
    private String expressCode = "-1";
    private DecimalFormat df = new DecimalFormat("######.###");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.dvp_2_0_activity_retund_service_detail);
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            refundId = bundle.getString("refundId");
            refundMark = bundle.getString("refundMark");
        }

        if (refundMark.equals(Constant.REQUEST_SUCCESS_0)) {
            logisticsLl.setVisibility(View.GONE);
            logisticsL.setVisibility(View.GONE);
            addressRl.setVisibility(View.GONE);
            setTitle("退款");
        } else {
            setTitle("退货退款");
        }
    }

    @Override
    protected void initData() {
        requestData();
    }

    private void requestData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("refundId", refundId);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.GET_REFUND_DETAIL, map, Url.GET_REFUND_DETAIL, new Callback<RefundServiceDetailBean>() {
            @Override
            public void onSuccess(RefundServiceDetailBean refundServiceDetailBean) {
                if (refundServiceDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                    switch (refundServiceDetailBean.getData().getRefundInfo().getRefundStatus()) {
                        case Constant.NUM_1 + "":
                            logisticsLl.setVisibility(View.GONE);
                            submit.setVisibility(View.GONE);
                            logisticsL.setVisibility(View.GONE);
                            addressRl.setVisibility(View.GONE);
                            addressLine.setVisibility(View.GONE);
                            status.setText("申请已提交");
                            time.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundTime());
                            message.setText("等待商家审核");
                            break;
                        case Constant.NUM_2 + "":
                            logisticsLl.setVisibility(View.VISIBLE);
                            submit.setVisibility(View.VISIBLE);
                            logisticsL.setVisibility(View.GONE);
                            addressRl.setVisibility(View.VISIBLE);
                            addressLine.setVisibility(View.VISIBLE);
                            submit.setText("提交");
                            status.setText("同意申请");
                            time.setText(refundServiceDetailBean.getData().getRefundInfo().getMerchantReviewTime());
                            message.setText("商家同意退货，请尽快填写退货物流信息。");
                            address.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundAddress());
                            break;
                        case Constant.NUM_3 + "":
                            logisticsLl.setVisibility(View.GONE);
                            submit.setVisibility(View.VISIBLE);
                            logisticsL.setVisibility(View.GONE);
                            addressRl.setVisibility(View.GONE);
                            addressLine.setVisibility(View.GONE);
                            submit.setText("联系客服");
                            status.setText("拒绝申请");
                            time.setText(refundServiceDetailBean.getData().getRefundInfo().getMerchantReviewTime());
                            message.setText("商家拒绝退货退款\n\n原因：" + refundServiceDetailBean.getData().getRefundInfo().getMerchantRefuseCase());
                            break;
                        case Constant.NUM_4 + "":
                            logisticsLl.setVisibility(View.GONE);
                            submit.setVisibility(View.GONE);
                            if (refundMark.equals(Constant.REQUEST_SUCCESS_0)) {
                                logisticsL.setVisibility(View.GONE);
                                addressRl.setVisibility(View.GONE);
                                addressLine.setVisibility(View.GONE);
                                status.setText("退款成功");
                            }else{
                                logisticsL.setVisibility(View.VISIBLE);
                                addressRl.setVisibility(View.VISIBLE);
                                addressLine.setVisibility(View.VISIBLE);
                                status.setText("退货退款成功");
                            }
                            time.setText(refundServiceDetailBean.getData().getRefundInfo().getMerchantRefundTime());
                            message.setText("商家同意退款");
                            logistic.setText(refundServiceDetailBean.getData().getRefundInfo().getExpressCompanyName());
                            logisticsNu.setText(refundServiceDetailBean.getData().getRefundInfo().getExpressNumber());
                            address.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundAddress());
                            break;
                        case Constant.NUM_5 + "":
                            logisticsLl.setVisibility(View.GONE);
                            submit.setVisibility(View.VISIBLE);
                            submit.setText("联系客服");
                            if (refundMark.equals(Constant.REQUEST_SUCCESS_0)) {
                                logisticsL.setVisibility(View.GONE);
                                addressRl.setVisibility(View.GONE);
                                addressLine.setVisibility(View.GONE);
                                status.setText("退款失败");
                            }else{
                                logisticsL.setVisibility(View.VISIBLE);
                                addressRl.setVisibility(View.VISIBLE);
                                addressLine.setVisibility(View.VISIBLE);
                                status.setText("退货退款失败");
                            }
                            time.setText(refundServiceDetailBean.getData().getRefundInfo().getMerchantRefundTime());
                            message.setText("商家拒绝退款\n\n原因：" + refundServiceDetailBean.getData().getRefundInfo().getMerchantRefuseCase());
                            logistic.setText(refundServiceDetailBean.getData().getRefundInfo().getExpressCompanyName());
                            logisticsNu.setText(refundServiceDetailBean.getData().getRefundInfo().getExpressNumber());
                            address.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundAddress());
                            break;
                        case Constant.NUM_6 + "":
                            logisticsLl.setVisibility(View.GONE);
                            submit.setVisibility(View.GONE);
                            if (!refundMark.equals(Constant.REQUEST_SUCCESS_0)) {
                                logisticsL.setVisibility(View.VISIBLE);
                                addressRl.setVisibility(View.VISIBLE);
                                addressLine.setVisibility(View.VISIBLE);
                            }else{
                                logisticsL.setVisibility(View.GONE);
                                addressRl.setVisibility(View.GONE);
                                addressLine.setVisibility(View.GONE);
                            }
                            status.setText("申请已提交");
                            time.setText(refundServiceDetailBean.getData().getRefundInfo().getExpressApplyTime());
                            message.setText("等待商家收货");
                            logistic.setText(refundServiceDetailBean.getData().getRefundInfo().getExpressCompanyName());
                            logisticsNu.setText(refundServiceDetailBean.getData().getRefundInfo().getExpressNumber());
                            address.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundAddress());
                            break;
                    }

                    if (!TextUtils.isEmpty(refundServiceDetailBean.getData().getRefundInfo().getRefundDescription())) {
                        instructionLl.setVisibility(View.VISIBLE);
                        instructionLine.setVisibility(View.VISIBLE);
                        instruction.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundDescription());
                    } else {
                        instructionLl.setVisibility(View.GONE);
                        instructionLine.setVisibility(View.GONE);
                    }

                    if (!TextUtils.isEmpty(refundServiceDetailBean.getData().getRefundInfo().getPhotoUrls())) {
                        takePhotoLl.setVisibility(View.VISIBLE);
                        String[] photos = refundServiceDetailBean.getData().getRefundInfo().getPhotoUrls().split(",");
                        switch (photos.length) {
                            case 1:
                                Glide.with(context)
                                        .load(photos[0])
                                        .error(R.mipmap.default_small_bg)
                                        .into(img1);
                                break;
                            case 2:
                                Glide.with(context)
                                        .load(photos[0])
                                        .error(R.mipmap.default_small_bg)
                                        .into(img1);
                                Glide.with(context)
                                        .load(photos[1])
                                        .error(R.mipmap.default_small_bg)
                                        .into(img2);
                                break;
                            case 3:
                                Glide.with(context)
                                        .load(photos[0])
                                        .error(R.mipmap.default_small_bg)
                                        .into(img1);
                                Glide.with(context)
                                        .load(photos[1])
                                        .error(R.mipmap.default_small_bg)
                                        .into(img2);
                                Glide.with(context)
                                        .load(photos[2])
                                        .error(R.mipmap.default_small_bg)
                                        .into(img3);
                                break;
                        }
                    } else {
                        takePhotoLl.setVisibility(View.GONE);
                    }

                    itemTitle.setText(refundServiceDetailBean.getData().getOrderDetailInfo().getMerchandiseName());
                    money.setText("￥" + df.format(refundServiceDetailBean.getData().getOrderDetailInfo().getMerchandisePrice() / 100));
                    List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(refundServiceDetailBean.getData().getOrderDetailInfo().getMcipValue(), ShopDetailMcipChildBean.class);
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < mcipList.size(); i++) {
                        if (i == mcipList.size() - 1) {
                            stringBuilder.append(mcipList.get(i).getPropertyValue());
                        } else {
                            stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                        }
                    }
                    numSpec.setText("数量：x" + refundServiceDetailBean.getData().getOrderDetailInfo().getPurchaseAmount() + ";" + stringBuilder.toString());
                    Glide.with(context)
                            .load(refundServiceDetailBean.getData().getOrderDetailInfo().getPhotoUrls().split(",")[0])
                            .error(R.mipmap.default_small_bg)
                            .into(photo);
                    //rcLayout.setClipBackground(true);
                    rcLayout.setTopLeftRadius(6);
                    rcLayout.setTopRightRadius(6);
                    rcLayout.setBottomLeftRadius(6);
                    rcLayout.setBottomRightRadius(6);

                    reason.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundCaseName());
                    moneyTv.setText("￥" + df.format(refundServiceDetailBean.getData().getRefundInfo().getRefundPrice() / 100));
                    refundTime.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundTime());
                    number.setText(refundServiceDetailBean.getData().getRefundInfo().getRefundNumber());

                } else {
                    showToast(refundServiceDetailBean.getMessage() + "");
                }
            }
        });
    }

    @Override
    @OnClick({R.id.logistics_rl, R.id.submit})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.logistics_rl:
                showDialog(this, null);
                finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.GET_COMPANY_LIST, null, Url.GET_COMPANY_LIST, new Callback<CompanyListBean>() {
                    @Override
                    public void onSuccess(CompanyListBean companyListBean) {
                        dismissDialog();
                        if (companyListBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                            DialogUtil.getInstance().showBottomDialog(context, R.layout.refund_select_reason);
                            View dialog_select_reason = DialogUtil.getInstance().dialog_bottom_layout;
                            dialog_select_reason.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_bottom.dismiss();
                                }
                            });
                            ListView listView = dialog_select_reason.findViewById(R.id.listview);
                            quickAdapter = new QuickAdapter<CompanyListBean.DataBean>(context, R.layout.item_string) {
                                @Override
                                protected void convert(BaseAdapterHelper helper, CompanyListBean.DataBean item) {
                                    helper.setText(R.id.content, item.getExpressName());
                                }
                            };
                            listView.setAdapter(quickAdapter);

                            quickAdapter.clear();
                            quickAdapter.addAll(companyListBean.getData());
                            quickAdapter.notifyDataSetChanged();

                            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    expressCode = quickAdapter.getItem(position).getExpressCode();
                                    logistics.setText(quickAdapter.getItem(position).getExpressName());
                                    selectTv.setVisibility(View.INVISIBLE);
                                    DialogUtil.getInstance().dialog_bottom.dismiss();
                                }
                            });
                        } else {
                            showToast(companyListBean.getMessage() + "");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissDialog();
                    }
                });
                break;
            case R.id.submit:
                if (submit.getText().toString().equals("提交")) {
                    if(!expressCode.equals("-1")&&!TextUtils.isEmpty(logisticsNum.getText().toString().trim())){
                        DialogUtil.getInstance().showCenterDialog(this, R.layout.dialog_shop_pay_success);
                        View dialog_layout = DialogUtil.getInstance().dialog_center_layout;

                        TextView title = dialog_layout.findViewById(R.id.title);
                        title.setText("确认物流信息是否正确？");
                        TextView message = dialog_layout.findViewById(R.id.message);
                        message.setVisibility(View.GONE);
                        dialog_layout.findViewById(R.id.sure_cancel).setVisibility(View.VISIBLE);

                        dialog_layout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DialogUtil.getInstance().dialog_center.dismiss();
                            }
                        });

                        dialog_layout.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DialogUtil.getInstance().dialog_center.dismiss();
                                showDialog(context,null);
                                HashMap<String,Object> map = new HashMap();
                                map.put("id",refundId);
                                map.put("expressCompanyCode",expressCode);
                                map.put("expressNumber",logisticsNum.getText().toString());
                                finalOkGo.requestEncrypt(RequestType.POSTFILESENCRYPT, Contacts.HOST_SERVER + Url.SUBMIT_REFUND_LOGISTIC, map, Url.SUBMIT_REFUND_LOGISTIC, new Callback<RefundServiceBean>() {
                                    @Override
                                    public void onSuccess(RefundServiceBean refundServiceBean) {
                                        dismissDialog();
                                        if (refundServiceBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                                            requestData();
                                        } else {
                                            showToast(refundServiceBean.getMessage() + "");
                                        }
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }
                                });
                            }
                        });
                        dialog_layout.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DialogUtil.getInstance().dialog_center.dismiss();
                            }
                        });
                    }else{
                        showToast("请选择快递公司和填写快递单号");
                    }

                } else if (submit.getText().toString().equals("联系客服")) {
                    call(Constant.phoneNum);
                }
                break;
        }
    }
}
